﻿using System.IO;
using Qiniu.Http;
using Qiniu.Storage;
using Qiniu.Util;
using UnityEditor;
using UnityEngine;

namespace Framework.Editor
{

    public class Qiniuyun
    {
        private static string ak = "5Su__7c8DSLC4PFvw5nWPVb_9RFAfAyp6YzumD2w";
        private static string sk = "K3BdGz0RYO_GoGKq9I_rJws0vqK4OTdHalscoaKW";
        private static string bucketName = "cnd-zweiluan";
        static Zone zone=Zone.ZONE_CN_South;
        private static string packageName = "DefaultPackage";
        private static string rootPath = $"{Application.dataPath}/../Bundles/{UnityEditor.EditorUserBuildSettings.activeBuildTarget }/{packageName}/v1.0";

        private static string buckerRoot = "new_game/v1.0/";
        [MenuItem("MyTool/Move")]
        public static void Move()
        {
            
        }

        static string GenToken()
        {
            Mac mac = new Mac(ak, sk);
            PutPolicy putPolicy = new PutPolicy();
            
            putPolicy.Scope =bucketName ;
            putPolicy.SetExpires(60);
           return Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
        }
        [MenuItem("MyTool/QuniuYun")]
        public static void UpDate()
        {
            Mac mac = new Mac(ak, sk);
            Config config = new Config();
            config.Zone = zone;
            BucketManager manager = new BucketManager(mac, config);
            UploadManager um = new UploadManager(config);
            string token = GenToken();
            PutExtra putExtra = null;
            HttpResult result;
            // Debug.Log(rootPath);
            foreach (var fileName in GetAllFile())
            {
                string saveKey = buckerRoot+Path.GetFileName(fileName);
                if (fileName.EndsWith(".bundle"))
                {
                    // manager.Stat(bucketName, saveKey);
                    StatResult sr = manager.Stat(bucketName, saveKey);
                    if (sr.Code == 612)
                    {
                        result = um.UploadFile(fileName, saveKey, token,putExtra);
                        if (result.Code != 200)
                        {
                            Debug.Log(result);
                        }
                        continue;
                    }
                    
                    
                }

                if (fileName.EndsWith(".hash"))
                {
                     result = um.UploadFile(fileName, saveKey, token,putExtra);
                     if (result.Code != 200)
                     {
                         Debug.Log(result);
                     }
                    continue;
                }

                if (fileName.EndsWith(".json"))
                {
                     result = um.UploadFile(fileName, saveKey, token,putExtra);
                     if (result.Code != 200)
                     {
                         Debug.Log(result);
                     }
                    continue;
                }
                if (fileName.EndsWith(".version"))
                {
                     result = um.UploadFile(fileName, saveKey, token,putExtra);
                     if (result.Code != 200)
                     {
                         Debug.Log(result);
                     }
                    continue;
                }
                if (fileName.EndsWith(".bytes"))
                {
                    result = um.UploadFile(fileName, saveKey, token,putExtra);
                    if (result.Code != 200)
                    {
                        Debug.Log(result);
                    }
                    
                    continue;
                }
                result = um.UploadFile(fileName, saveKey, token,putExtra);
                if (result.Code != 200)
                {
                    Debug.Log(result);
                }

            }
        }

        static string[] GetAllFile()
        {
             return Directory.GetFiles(rootPath);
        }
    }

}